<?php
/**
 * 导出excel
 */

namespace App\Services;

use App\Exceptions\AppException;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Services\HandleDataService;


class ExcelExportService
{
    /**
     * @param $directory //文件夹
     * @param $fileName //文件名
     * @param $build    //build
     * @param $headerColumn //表头
     * @param string $handle    //调用handle
     * @param int $chunkNum
     * @param array $widthArray //宽度
     * @return bool
     */
    public function exportDataBySheet($directory, $fileName, $build, $headerColumn,$handle = '', $chunkNum = 1000, $widthArray = [])
    {
        $service = new \App\Services\HandleDataService();
        try {
            ini_set('memory_limit','1024M');
            @mkdir($directory);
            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

            $sheet = $spreadsheet->getActiveSheet();
            $sheet->getPageSetup()->setHorizontalCentered(true);
            //设置单元格宽度
            if($widthArray){
                foreach ($widthArray as $key => $value){
                    $sheet->getColumnDimension($key)->setWidth($value);
                }
            }
            //设置单元格内容
            $titCol = 'A';
            foreach ($headerColumn as $key => $value) {
                // 单元格内容写入
                $sheet->setCellValue($titCol . '1', $value);
                $titCol++;
            }
            $row = 2; // 从第二行开始
            $build->chunk($chunkNum, function ($result) use($headerColumn, $handle, $sheet, $service, &$row){
                $data = $result->toArray();
                if($handle != ''){
                    $data = $service->$handle($data);
                }
                foreach($data as $key => $value){
                    $res = [];
                    foreach ($headerColumn as $kh => $vh){
                        $res[$kh] = $value[$kh];
                    }
                    $dataCol = 'A';
                    foreach ($res as $item) {
                            $sheet->setCellValue($dataCol . $row, $item);
                            $dataCol++;
                    }
                    $row++;
                }
            });
            $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
            $writer->save($directory."/".$fileName);
            return true;
        } catch (\Exception $e){

            throw new AppException('导出异常:' . $e->getLine() .$e->getMessage() , 500);
        }
    }
    /**
     * @param $directory
     * @param $fileName
     * @param $data
     * @param $headerColumn
     * @param string $handle
     * @param int $chunkNum
     * @param array $widthArray
     * @return bool
     */
    public function exportDataArrayBySheet($directory, $fileName, $data, $headerColumn,$handle = '', $chunkNum = 1000, $widthArray = [])
    {
        try {
            ini_set('memory_limit','1024M');
            @mkdir($directory);
            $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();
            $sheet->getPageSetup()->setHorizontalCentered(true);
            //设置单元格宽度
            if($widthArray){
                foreach ($widthArray as $key => $value){
                    $sheet->getColumnDimension($key)->setWidth($value);
                }
            }
            //设置单元格内容
            $titCol = 'A';
            foreach ($headerColumn as $key => $value) {
                // 单元格内容写入
                $sheet->setCellValue($titCol . '1', $value);
                $titCol++;
            }
            $row = 2; // 从第二行开始

            if($handle != ''){
                $data = $this->handleDataService->$handle($data);
            }
            foreach($data as $key => $value){
                $res = [];
                foreach ($headerColumn as $kh => $vh){
                    $res[$kh] = $value[$kh];
                }
                $dataCol = 'A';
                foreach ($res as $item) {
                    $sheet->setCellValue($dataCol . $row, $item);
                    $dataCol++;
                }
                $row++;
            }
            $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
            $writer->save($directory."/".$fileName);
            return true;
        } catch (\Throwable $e){
            throw new AppException('导出异常' . $e->getMessage());
        }
    }

    public function export()
    {
        $handle = new HandleDataService();
        $res = $handle->handleCallExportData();
    }
}